<?php
/**
 * [WeEngine System] Copyright (c) 2013 WE7.CC
 * User: fanyk
 * Date: 2017/10/14
 * Time: 15:10
 */

namespace Ken\Cos4;


class Cos4Stream {

	const WRAPPER_NAME = 'cos4';
	private $stream;

	private $path;
	public static function register() {
		stream_register_wrapper(self::WRAPPER_NAME, get_class());
	}



	public function stream_open($path, $mode, $options, &$opened_path)
	{
		$this->path = $this->clearPrefix($path);
		return true;
	}

	public function stream_read($count)
	{

	}

	public function stream_eof()
	{
		return true;
	}


	public function stream_write($data) {
		$this->api()->put($this->path, $data);
	}

	public function unlink($path) {
		$this->api()->unlink($this->clearPrefix($path));
	}

	private function api() {
		return new Cos4Api();
	}

	private function clearPrefix($path) {
		return str_replace(self::WRAPPER_NAME, '', $path);
	}
}